﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.EntityCode;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Enum;
using RooHui.Infrastructure.Entities;

namespace RooHui.OA.Biz
{
    public class ModuleBiz
    {
        /// <summary>
        /// 获取所有模块信息
        /// </summary>
        /// <returns></returns>
        public static List<Module> GetModelList()
        {
            try
            {
                var moduleList = new Module().FindAll();
                return moduleList;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static List<Module> GetModelListByUser(string userName)
        {
            return iCache.Get<List<Module>>(String.Format("ModuleBiz.GetModelListByUser:{0}", userName),
                () =>
                {
                    using (Context ctx = new Context())
                    {
                        var loginUser = new User().Find(System.Web.HttpContext.Current.User.Identity.Name);
                        if (loginUser == null)
                        {
                            return null;
                        }
                        if (loginUser.Status == ItemStatus.Supper)
                        {
                            var data = new Module().FindAll();
                            data.ForEach(c =>
                            {
                                if (!String.IsNullOrEmpty(c.Url))
                                {
                                    c.Url = c.Url.ToLower();
                                }
                            });
                            return data;
                        }
                        else
                        {
                            var data = (from c in ctx.Module
                                        join d in ctx.RoleModule
                                         on c.ID equals d.ModuleID
                                        join e in ctx.Role
                                        on d.RoleID equals e.ID
                                        join f in ctx.UserRole
                                        on e.ID equals f.RoleID
                                        join g in ctx.User
                                        on f.UserName equals g.UserName
                                        where g.UserName == userName
                                        select new { c }).Distinct()
                                            .ToList()
                                            .Select(e =>
                                            {
                                                var module = new Module().Set(e.c);
                                                module.Url = module.Url.ToLower();
                                                return module;
                                            })
                                            .ToList();
                            return data;
                        }
                    }
                }, 600);
        }
    }
}
